package com.yuanfeng.base.service.impl;


import com.yuanfeng.base.mapper.BusinessSwitchMapper;
import com.yuanfeng.base.service.BusinessSwitchService;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
/**
 * @ClassName BusinessSwitchServiceImpl
 * @Description
 * @Author cyp
 * @Date 2023-06-13 09:05:20
 * @Version 1.0
 **/
@Service
public class BusinessSwitchServiceImpl implements BusinessSwitchService {

    private static final Logger LOG = LoggerFactory.getLogger(BusinessSwitchServiceImpl.class);

    @Autowired
    private BusinessSwitchMapper businessSwitchMapper;


    @Override
    public ResponseResult query(Map<String, Object> param) {
        ResponseResult resp = new ResponseResult();
        try {
            Integer result = businessSwitchMapper.query(param);
            return ResponseResult.success(CommonStatusEnum.SUCCESS.getCode(), BizCodeEnume.OPERATION_SUCCESSFUL.getMsg(), result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(), BizCodeEnume.SYSTEM_ERROR.getMsg());
        }
    }

    @Override
    public ResponseResult update(Map<String, Object> param) {
        try {
            businessSwitchMapper.updateValue(param);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(), BizCodeEnume.SYSTEM_ERROR.getMsg());
        }
    }

    @Override
    public ResponseResult queryPwdConfig(Map<String, Object> param) {
        try {
            param.put("configKey", "password_format");
            String passwordFormat = businessSwitchMapper.queryValue(param);
            param.put("configKey", "password_length");
            String passwordLength = businessSwitchMapper.queryValue(param);
            param.clear();
            param.put("passwordLength", passwordLength);
            param.put("passwordFormat", passwordFormat);
            return ResponseResult.success(param);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(), BizCodeEnume.SYSTEM_ERROR.getMsg());
        }
    }

    @Override
    public ResponseResult updatePwdConfig(Map<String, Object> param) {
        try {
            param.put("configKey", "password_format");
            param.put("configValue", param.get("passwordFormat"));
            //修改密码组合
            businessSwitchMapper.updateValue(param);
            param.put("configKey", "password_length");
            param.put("configValue", param.get("passwordLength"));
            //修改密码长度
            businessSwitchMapper.updateValue(param);
            return ResponseResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(),BizCodeEnume.SYSTEM_ERROR.getMsg());
        }
    }
}
